home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 3358 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  102 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: fuw.edu.pl!cyfronet!student!iskra
  3. From: iskra@student.uci.agh.edu.pl (Kamil Iskra)
  4. Subject: Re: Return types and virtual methods.
  5. Message-ID: <DMIE92.9Lv@cyf-kr.edu.pl>
  6. Sender: news@cyf-kr.edu.pl (News Administrator)
  7. Nntp-Posting-Host: student.uci.agh.edu.pl
  8. Organization: Academic Computer Centre, CYFRONET
  9. X-Newsreader: TIN [version 1.1 PL8]
  10. References: <4f4qbv$q18@columba.udac.uu.se>
  11. Date: Fri, 9 Feb 1996 12:48:38 GMT
  12.  
  13. Daniel Widenfalk (t94dwi@chaph.tdb.uu.se) wrote:
  14. > class Set {
  15. >     public:
  16. >     virtual    ~Set();
  17.  
  18. >     virtual Set & operator=(const Set &) = 0;
  19. >     virtual Set operator~() = 0;
  20. >     ...
  21. > };
  22.  
  23. > class BitSet : public set {
  24. >     public:
  25. >         ~BitSet();
  26.  
  27. >     BitSet & operator=(const BitSet &);
  28. >     BitSet operator~();
  29. >     ...
  30. > };
  31.  
  32. > Now, my question is. How do I fix this? I want to be able to have several
  33. > diffrent representations of sets, but I want them to look the same to the
  34. > user.
  35.  
  36. I've seen similar question on comp.lang.c++.moderated (which would be more
  37. appropriate for your question, BTW). Here's one of the answers for that
  38. question: 
  39.  
  40. From: John Max Skaller <maxtal@suphys.physics.su.oz.au>
  41. Newsgroups: comp.lang.c++.moderated
  42. Subject: Re: How to implement virtual operator== w/o RTTI?
  43. Date: 5 Feb 1996 03:41:44 -0000
  44.  
  45. Phil Verghese wrote:
  46. > I'm trying to implement a virtual operator==, and I'd like to avoid
  47. > having to use RTTI in the derived classes.  
  48.  
  49.     Forget it. It cannot work. Any attempt to to even
  50. try to do it is doomed to be a perversion.
  51.  
  52.     It is not necassary either. A single global
  53. function is all that is needed, to compare two abstract
  54. classes of the same type. Such comparison must call on
  55. the virtual accessor functions to obtain the state of the
  56. two objects for the comparison.
  57.  
  58.     If the state cannot be accessed this way,
  59. you have already broken principles of good design.
  60.  
  61.     In particular, it is a complete fallacy that
  62. because two derived classes have a different type
  63. they should compare not equal. Derived classes are
  64. best used to implement abstractions. Two distinct
  65. derived classes can easily implement the SAME subtype
  66. of the base abstraction.
  67.  
  68.     For example:
  69.  
  70.     struct Rectangle {
  71.         virtual double getLength()const =0;
  72.         virtual double getBreadth()const =0;
  73.     };
  74.  
  75.     bool operator == (Rectangle const &a, Rectangle const &b) {
  76.         return 
  77.             a.getLength() == b.getLength() &&
  78.             a.getBreadth() == b.getBreadth();
  79.     }
  80.  
  81. Note this works NO MATTER WHAT IMPLEMENTATION OF Rectangle is
  82. used. For example it will correctly compare "ConcreteSquare" 
  83. with "ConcreteRectangle".
  84.  
  85. -- 
  86. John Max Skaller               voice: 61-2-566-2189
  87. 81 Glebe Point Rd              fax:   61-2-660-0850
  88. GLEBE NSW 2037                 web: http://www.maxtal.com.au/~skaller/
  89. AUSTRALIA                      email: skaller@maxtal.com.au
  90.  
  91.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  92.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  93.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  94.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  95.  
  96. --
  97. / Kamil Iskra - AMIGA 1200, 68030 50MHz, HDD 850 MB, 10 MB RAM \
  98. | iskra@student.uci.agh.edu.pl  kiskra@ernie.icslab.agh.edu.pl |
  99. | http://student.uci.agh.edu.pl/~iskra                         |
  100. \ PGP public key available via Finger or WWW                   /
  101.